/**
 * @api post model.
 *
 * User: yunshengzhu
 * Date: 2020/12/3
 * Time: 下午2:09
 */
package model

func CreatePro(obj ProDb) (err error) {
	return db.Create(&obj).Error
}

func UpdatePro(obj ProDb) (err error) {
	return db.Exec(`update t_pro set pro_name=?,price=?,content=?,online=?,img=?,read_num=?,sold=?,socket=?,status=?,excuse=?,check_time=? where sku=?`,
		obj.ProName, obj.Price, obj.Content, obj.Online, obj.Img, obj.ReadNum, obj.Sold, obj.Stock, obj.Status, obj.Excuse, obj.CheckTime, obj.Sku).Error
}

func GetProSkuList(skuList []int64) (list []VProDb, err error) {
	list = make([]VProDb, 0)
	return list, db.Where("sku in  ?", skuList).Find(&list).Error
}

func ProList(mid int64, key string, status, online, page, size int) (list []ProDb, err error) {
	list = make([]ProDb, 0)
	tx := db
	if mid > 0 {
		tx = tx.Where("mid=?", mid)
	}
	if status >= 0 {
		tx = tx.Where("status=?", status)
	}

	if online >= 0 {
		tx = tx.Where("online=?", online)
	}

	if len(key) > 0 {
		tx = tx.Where("pro_name like ?", "%"+key+"%")
	}
	return list, tx.Limit(size).Offset((page - 1) * size).Find(&list).Error
}

func ProCount(mid int64, key string, status, online int) (int64, error) {
	count := ProCountDb{}
	tx := db
	if mid > 0 {
		tx = tx.Where("mid=?", mid)
	}

	if status >= 0 {
		tx = tx.Where("status=?", status)
	}

	if online >= 0 {
		tx = tx.Where("online=?", online)
	}

	if len(key) > 0 {
		tx = tx.Where("pro_name like ?", "%"+key+"%")
	}
	return count.Count, tx.Select("count(*) as count").Take(&count).Error
}

func GetProSku(sku int64) (obj ProDb, err error) {
	return obj, db.Where("sku=?", sku).Take(&obj).Error
}

func UpdateMerchantMinPrice(mid int64) error {
	count := ProCountDb{}
	err := db.Select("min(price) as count").Where("status=1 and mid=?", mid).Take(&count).Error
	if err != nil {
		return err
	}
	return db.Exec(`update t_merchant set min_price=? where mid=?`, count.Count, mid).Error
}

func GetVProList(online int, mid int64, page, size int, lon, lat string, order int) (list []VProDb, err error) {
	list = make([]VProDb, 0)
	tx := db
	if mid > 0 {
		tx = tx.Where("mid=?", mid)
	}

	if online >= 0 {
		tx = tx.Where("online=?", online)
	}
	// 排序 1价格升序 2价格降序 3销量升序 4销量降序 5距离升序 6距离降序
	switch order {
	case 1:
		tx = tx.Order("price asc")
	case 2:
		tx = tx.Order("price desc")
	case 3:
		tx = tx.Order("sold asc")
	case 4:
		tx = tx.Order("sold desc")
	case 5:
		tx = tx.Order("distance asc")
	case 6:
		tx = tx.Order("distance desc")
	}

	if online == 0 {
		tx = tx.Select("*,round((st_distance(point(lon, lat),point(?, ?)) / 0.0111) * 1000)AS distance", lon, lat)
	}

	err = tx.Offset((page - 1) * size).Limit(size).Find(&list).Error
	return list, err
}

func GetVProCount(online int, mid int64) (num int64, err error) {
	tx := db
	if mid > 0 {
		tx = tx.Where("mid=?", mid)
	}

	if online >= 0 {
		tx = tx.Where("online=?", online)
	}

	count := VProCountDb{}
	err = tx.Select("count(*) as count").Take(&count).Error
	return count.Count, err
}

func DelPro(sku int64) error {
	return db.Exec(`delete from t_pro where sku=?`, sku).Error
}

func UpdateProStock(orderNumber string) error {
	list := make([]OrderDetailDb, 0)
	err := db.Where("order_number=?", orderNumber).Find(&list).Error
	if err != nil {
		return err
	}

	for _, v := range list {
		err = db.Exec(`update t_pro set sold=sold+? where sku=?`, v.Num, v.Sku).Error
		if err != nil {
			return err
		}
	}
	return nil
}
